<?php

class edit_banthresholds
{
	var $mess = '';
	
	var $data = Array();

	function edit_banthresholds()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
		
		
		if( ! isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			//Setup Edit
			global $db, $lang;
			
			$_POST['itemID'] = split(',', $_POST['itemID']);
			
			//get the threshold
			$q = "SELECT bt_points, bt_license, l_name, bt_duration, bt_cleared
				  FROM ". BANTHRESHOLDS_TABLE .  "
				  LEFT JOIN " . LICENSES_TABLE . " ON (bt_license = l_id)
				  WHERE bt_license = " . $_POST['itemID'][0] . " AND bt_points = " . $_POST['itemID'][1];
			$result = $db->sql_query($q);
			if($row = $db->sql_fetchrow($result)) 
			{
				$this->data = $row;
				$this->data['bt_id'] = $row['bt_license'] . ',' . $row['bt_points'];
				$this->data['PointsLocked'] = 'DISABLED';
				if($row['bt_license'] == 0)
				{
					$this->data['licenseOptions'] = '<option value="0" SELECTED>' . $lang['AllLicenses'] . '</option>';
				}
				else
				{
					$this->data['licenseOptions'] = '<option value="' . $row['bt_license'] . '">'. $row['l_name'] . "</option>";				
				}
				
				$this->data['mess'] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$this->data['valid'] = true;
				
			}
			else
			{
				$this->data['mess'] = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
				$this->data['valid'] = false;
			}
		}
		else
		{
			//setup add
			global $db, $lang;
			
			$this->data['bt_points'] = '0';
			$this->data['bt_duration'] = '0';
			$this->data['bt_cleared'] = '0';
			$this->data['PointsLocked'] = '';
			$this->data['bt_id'] = '';
								
			$licenseOptions = '<option value="0" SELECTED>' . $lang['AllLicenses'] . '</option>';
				
			//get the license
			$q = "SELECT l_id, l_name
				  FROM ". LICENSES_TABLE . "
				  ORDER BY l_order ASC";
				
			$result = $db->sql_query($q);
			while($l = $db->sql_fetchrow($result)) 
			{
				$licenseOptions .=  '<option value="' . $l['l_id'] . '">' . $l['l_name'] . '</option>';
			}
				
			$this->data['licenseOptions'] = $licenseOptions;
							
				
			$this->data['mess'] = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		
				
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		if(isset($_POST['editItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		$_POST['itemID'] = split(',', $_POST['itemID']);
		$qDel = "delete from ". BANTHRESHOLDS_TABLE . " Where bt_license = " . $_POST['itemID'][0] . " AND bt_points = " . $_POST['itemID'][1];
		$result = $db->sql_query($qDel);
		$this->mess = new addMessage(str_replace('{entry}', $lang['BanThreshold'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". BANTHRESHOLDS_TABLE . " (bt_license, bt_points, bt_duration, bt_cleared) 
						   values(" . $_POST['license'] . ", " . $_POST['points'] . ", " . $_POST['duration'] . ", " . $_POST['cleared'] . ")" ;
			$result = $db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', $lang['BanThreshold'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		$_POST['id'] = split(',', $_POST['id']);
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". BANTHRESHOLDS_TABLE . " SET bt_duration = " . $_POST['duration'] .
												", bt_cleared = " . $_POST['cleared'] .
											  " WHERE bt_license = " . $_POST['id'][0] .
											  		 " AND bt_points = " . $_POST['id'][1];
			echo $qUpd;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $lang['BanThreshold'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = sizeof($_POST['id']) == 2 && is_numeric($_POST['id'][0]) && is_numeric($_POST['id'][1]);
		}
		
		$valid = $valid && is_numeric( $_POST['duration']);
		
		return $valid;
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $lang;
		
		$this->data['mess']->render();
		
		if($this->data['valid'])
		{
			$data = $this->data;
					
			$addS = '';
			$addE = '';
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
					
			include $skin_dir . 'admin/editbanthresholds.tpl';
		}
	}

	function renderAdd()
	{
		global $skin_dir, $lang;
		
		$this->data['mess']->render();
		$data = $this->data;
					
		$addS = '<!--';
		$addE = '-->';
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
					
		include $skin_dir . 'admin/editbanthresholds.tpl';
	}
	
}
?>